import { createRouter, createWebHistory,createWebHashHistory } from 'vue-router';

const whiteUrl = ['/login','/reg'];
const routes = [
    {
    path: '/login',
    meta:{tiltle:'登录'},
    
    component: ()=>import('../views/login/Login.vue') //路由懒加载
  },
  {
    path: '/reg',
    meta:{tiltle:'生命周期'},
    
    component: ()=>import('../views/login/Reg.vue') //路由懒加载
  },
  {
    path: '/',
    redirect: '/manage',
 
  },
  {
    path: '/manage',
    meta:{tiltle:'首页'},
    component: ()=>import('../views/home/Home.vue'),
    children:[
        {
            path: 'info',
            component: ()=>import('../views/user/UserInfo.vue'),
        },
        {
            path: 'fee',
            meta:{tiltle:'缴费'},
            component: ()=>import('../views/fee/Fee.vue'),
        },
        {
            path: 'news',
            meta:{tiltle:'公告'},
            component: ()=>import('../views/news/News.vue'),
        },
        {
            path: 'news/edit/:id/:type',
            meta:{tiltle:'修改公告'},
            component: ()=>import('../views/news/Edit.vue'),
        }
    ]
  }
  
]
const router = createRouter({

    // history: createWebHistory(), //history模式
    history: createWebHashHistory(), //hash模式
    routes

})//创建路由实例

//路由全局守卫  路由前置守卫 beforeEach
router.beforeEach((to, from) => { 

    // console.log("to前往的页面",to);
    // console.log("from从哪个页面过来",from);
    // console.log("next放行");
    //next(); //router4 可以使用retrun 代替 next()

    //return true; // return true  或不 return 放行

    //如果是登录页面 直接放行
    if(whiteUrl.includes(to.path)){
        return true;
    }

    let token=sessionStorage.getItem("token");  
    if(!token){
        //   next("/login");
          return  "/login";
    }

    

})
//全局守卫  路由后置守卫
router.afterEach((to)=>{ 

    document.title=to.meta.tiltle || '智慧社区';
})


export default router;